import { createPinia } from 'pinia';
import { cloneDeep } from 'lodash-es';

const pinia = createPinia();

const piniaReset = ({ store }) => {
  let initialState = cloneDeep(store.$state);
  store.$reset = () => {
    store.$patch(($state) => Object.assign($state, initialState));
  };
};

pinia.use(piniaReset);

pinia.$reset = function () {
  pinia._s.forEach((item) => {
    try {
      item.$reset();
    } catch (e) {
      console.log(e);
    }
  });
};

export default pinia;
